<!--
 * @Description:封装可视区域宽度
 * @Author: Sun
 * @Date: 2019-07-01 20:05:50
 * @LastEditors: Sun
 * @LastEditTime: 2019-07-21 18:12:01
 -->
<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>封装可视区域宽度</title>
	<style>
		* {
			margin: 0;
			padding: 0
		}

		htem,
		body {
			height: 100%;
		}

		button {
			border: 1px solid eee;
			padding: 5px 10px;
		}

		div {
			width: 100px;
			height: 100px;
			background-color: pink;
			position: absolute;
			left: 0px;
			top: 50px;
		}
	</style>
</head>

<body>
	<button id="btn200"> 200</button>
	<button id="btn400">400</button>
	<div id="box">点击按钮</div>
	<script>
		function $(id) {
			return document.getElementById(id);
		}
		window.onload = function () {
			function client() {
				if (window.innerWidth != null) { //ie9+
					return {
						width: window.innerWidth,
						height: window.innerHeight
					}
				} else if (document.compatMode == "CSS1Compat") {
					return { //不是怪异模式浏览器 <!DOCTYPE html> 标准模式
						width: document.documentElement.clientWidth,
						height: document.documentElement.clientHeight
					}
				} else {
					return { //怪异模式浏览
						width: document.body.clientWidth,
						height: document.body.clientHeight
					}
				}
			}

			resize()
			window.onresize = resize;
			function resize() {
				console.log(client())
				let clientWidth = client().width
				if (clientWidth > 960) {
					document.body.style.backgroundColor = "red"
					console.log("11")
				} else if (clientWidth > 640) {
					document.body.style.backgroundColor = "black"
					console.log("22")
				} else {
					document.body.style.backgroundColor = "blue"
					console.log("33")
				}
			}




		}
	</script>

</body>

</html>